package cn.jinjimi.app.main.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import cn.jinjimi.base.util.LogUtil;
import cn.jinjimi.app.main.extend.BaseFragment;

import java.util.List;


/**
 *
 * @date 10/15/14
 */
public class FragmentPageAdapter extends FragmentStatePagerAdapter {

    public static final String TAG = FragmentPageAdapter.class.getName();

    private List<? extends Fragment> pageFragments;
    private List<String> listTitle;

    private FragmentManager manager;

    public FragmentPageAdapter(FragmentManager fm, List<? extends Fragment> pageFragments) {
        super(fm);
        this.manager = fm;
        this.pageFragments = pageFragments;
    }

    public FragmentPageAdapter(FragmentManager fm, List<? extends Fragment> pageFragments, List<String> listTitle) {
        super(fm);
        this.pageFragments = pageFragments;
        this.listTitle = listTitle;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (listTitle != null && listTitle.size() > 0)
            return listTitle.get(position);
        return "";
    }

    @Override
    public Fragment getItem(int i) {
        LogUtil.d(TAG, pageFragments.get(i).toString());
        return pageFragments.get(i);
    }

    @Override
    public int getCount() {
        return pageFragments.size();
    }

    public void removeAll() {
        if (pageFragments != null && pageFragments.size() > 0) {
            for (Fragment pagerFragment : pageFragments) {
                manager.beginTransaction().remove(pagerFragment).commit();
            }
        }
    }
}
